package src.main.kotlin.day08

/**
 * 初始化块  init{}
 *   可以设置变量或者值，以及执行有效性检查，如检查传给某构造函数的值是否有效
 *   初始化块代码执行时机：在构造类实例时执行
 *   static{}  静态代码块是在类加载的适合初始化
 *
 * TODO: 类的初始化顺序
 *  1. 主构造函数里声明的属性
 *  2. 类级别的属性赋值
 *  3. init初始化块里的属性赋值和函数调用
 *  4. 次构造函数里的属性赋值和函数调用
 *
 *
 */
fun main() {

//    val p1 = Person( age = -10, isStudy = true)
//    val p2 = Person( "",age = 10, isStudy = true)
//    val p3 = Person( "",10, true)
    val p3 = Person( "JAMES",-10, true)
    println(p3.name)
    println(p3.age)
}

class Person(
    _name:String,
    var age:Int = 10,
    var isStudy:Boolean
){
    var name = _name
        get() = field.capitalize()
        set(value) {
            field = value.trim()
        }

    constructor(_name: String) : this(_name,age = 12, isStudy = false)
    constructor(age: Int) : this(_name = "rose",age,isStudy=false)
    constructor(isStudy: Boolean) : this(_name = "lily",age = 13,isStudy)

    constructor(_name: String,age: Int) : this(_name,age, isStudy = false)

    constructor(age: Int,isStudy: Boolean) : this(_name = "linlin",age,isStudy)

    init {
        require(age > 0){"age must be positive"}
        require(_name.isNotBlank()){"Person must have a name"}
    }

}